package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.mapper.StudentsMapper;
import com.example.mybatisplus.model.domain.Logs;
import com.example.mybatisplus.model.domain.Students;
import com.example.mybatisplus.model.dto.TimeRangeRequest;
import com.example.mybatisplus.model.dto.VioDetailDto;
import com.example.mybatisplus.model.dto.VioNumberDTO;
import com.example.mybatisplus.service.LogsService;
import com.example.mybatisplus.service.StudentsService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.ViolationsService;
import com.example.mybatisplus.model.domain.Violations;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2024-07-17
 * @version v1.0
 */
@Controller
@RequestMapping("/api/violations")
public class ViolationsController {

    private final Logger logger = LoggerFactory.getLogger( ViolationsController.class );


    @Autowired
    private ViolationsService violationsService;
    @Autowired
    private LogsService logsService;
    @Autowired
    private StudentsService studentsService;

    @PostMapping("/insert")
    @ResponseBody
    @Transactional
    public JsonResponse<VioDetailDto> insertViolations(@RequestBody VioDetailDto vioDetailDto){
        try {
            Violations violations=vioDetailDto.toViolation();
            Students students= studentsService.getStudentById(vioDetailDto.getStudentId());
            violations.setCollege(students.getCollege());
            violations.setName(students.getName());
            violations.setMajor(students.getMajor());
            violations.setGrade(students.getGrade());
            violationsService.insertAll(violations);
            Logs logs=new Logs();
            logs.setVioId(vioDetailDto.getVioId());
            logs.setOperationTime(LocalDateTime.now());
            logs.setOperatorAid(Long.valueOf(vioDetailDto.getOperatorAid()));
            logs.setOperationType("创建记录");
            logsService.insertLog(logs);
            return JsonResponse.success(vioDetailDto,"插入成功");
        } catch (Exception e) {
            logger.error("记录失败", e);
            return JsonResponse.message(false, 420, "记录失败");
        }
    }

    @GetMapping("/getAll")
    @ResponseBody
    public JsonResponse<List<Violations>> getAllViolations(Violations violations) {
        try {
            List<Violations> violationsList = violationsService.getAllViolations(violations);
            return JsonResponse.success(violationsList, "获取违纪记录成功");
        } catch (Exception e) {
            logger.error("获取违纪记录失败", e);
            return JsonResponse.message(false, 500, "获取违纪记录失败");
        }
    }

    @GetMapping("/getAll2")
    @ResponseBody
    public JsonResponse<List<Violations>> getAllViolationsNot() {
        try {
            List<Violations> violationsList = violationsService.getAllViolationsNot();
            return JsonResponse.success(violationsList, "获取违纪记录成功");
        } catch (Exception e) {
            logger.error("获取违纪记录失败", e);
            return JsonResponse.message(false, 500, "获取违纪记录失败");
        }
    }

    @GetMapping("/getNumber")
    @ResponseBody
    public JsonResponse<VioNumberDTO> getVioNumbers() {
        List<Integer> typeNumbers = violationsService.getTypeNumber();
        List<VioNumberDTO.CollegeData> collegeData = violationsService.getCollegeData();

        VioNumberDTO vioNumberDTO = new VioNumberDTO(typeNumbers, collegeData);

        return JsonResponse.success(vioNumberDTO, "Success");
    }

    @PostMapping("/getNumberByTimeRange")
    @ResponseBody
    public JsonResponse<VioNumberDTO> getVioNumbersByTimeRange(@RequestBody TimeRangeRequest request) {
        LocalDateTime startTime = request.getStartTime();
        LocalDateTime endTime = request.getEndTime();

        List<Integer> typeNumbers = violationsService.getTypeNumberByTimeRange(startTime, endTime);
        List<VioNumberDTO.CollegeData> collegeData = violationsService.getCollegeDataByTimeRange(startTime, endTime);

        VioNumberDTO vioNumberDTO = new VioNumberDTO(typeNumbers, collegeData);

        return JsonResponse.success(vioNumberDTO, "Success");
    }


    @PostMapping("exportUser")
    public void exportUser(HttpServletResponse response){
        violationsService.exportUser(response);
    }

    @PostMapping("exportUser2")
    public void exportUser2(HttpServletResponse response){
        violationsService.exportUser2(response);
    }

    @GetMapping("/getAllMajor")
    @ResponseBody
    public JsonResponse<List<String>> getAllMajor() {
        try {
            List<String> major = violationsService.getMajor();
            return JsonResponse.success(major, "获取专业成功");
        } catch (Exception e) {
            logger.error("获取专业失败", e);
            return JsonResponse.message(false, 500, "获取专业失败");
        }
    }

    @GetMapping("/getAllKind")
    @ResponseBody
    public JsonResponse<List<String>> getAllKind() {
        try {
            List<String> major = violationsService.getKind();
            return JsonResponse.success(major, "获取违纪种类成功");
        } catch (Exception e) {
            logger.error("获取失败", e);
            return JsonResponse.message(false, 500, "获取失败");
        }
    }

    @GetMapping("/getAllAcade")
    @ResponseBody
    public JsonResponse<List<String>> getAllAcade() {
        try {
            List<String> major = violationsService.getAcade();
            return JsonResponse.success(major, "获取学院成功");
        } catch (Exception e) {
            logger.error("获取失败", e);
            return JsonResponse.message(false, 500, "获取失败");
        }
    }

    @PostMapping("importViolations")
    @ResponseBody
    public JsonResponse importWhitelist(@RequestBody MultipartFile file) {
        try {
            violationsService.importWhitelistSettings(file);
            return JsonResponse.success(true, "文件上传成功");
        } catch (Exception e) {
            logger.error("文件上传失败", e);
            return JsonResponse.message(false, 500, "文件上传失败");
        }
    }

    @PostMapping("/deleteViolations")
    @ResponseBody
    @Transactional
    public JsonResponse deleteViolation(@RequestBody VioDetailDto vioDetailDto) {
        try {
            violationsService.deleteViolation(vioDetailDto.getVioId());
            Logs logs=new Logs();
            logs.setVioId(vioDetailDto.getVioId());
            logs.setOperationTime(LocalDateTime.now());
            logs.setOperatorAid(Long.valueOf(vioDetailDto.getOperatorAid()));
            logs.setOperationType("解除违纪");
            logsService.insertLog(logs);
            return JsonResponse.success(true, "解除违纪成功");
        } catch (Exception e) {
            logger.error("解除违纪失败", e);
            return JsonResponse.message(false, 500, "解除违纪失败");
        }
    }

    @PostMapping("/extendViolations")
    @ResponseBody
    @Transactional
    public JsonResponse extendViolation(@RequestBody VioDetailDto vioDetailDto){
        try {
            violationsService.extendViolation(vioDetailDto.getVioId(),vioDetailDto.getPublishmentDuration());
            Logs logs=new Logs();
            logs.setVioId(vioDetailDto.getVioId());
            logs.setOperationTime(LocalDateTime.now());
            logs.setOperatorAid(Long.valueOf(vioDetailDto.getOperatorAid()));
            logs.setOperationType("延长违纪时间");
            logsService.insertLog(logs);
            return JsonResponse.success(true, "延长违纪成功");
        } catch (Exception e) {
            logger.error("更改时间失败", e);
            return JsonResponse.message(false, 500, "更改违纪失败");
        }
    }
    @GetMapping("/getAllCategory")
    @ResponseBody
    public List<String> getAllCategory() {
        try {
            List<String> category = violationsService.getVioType();
            return category;
        } catch (Exception e) {
            logger.error("获取失败", e);
            return null;
        }
    }

    @GetMapping("/getAllCategoryNum")
    @ResponseBody
    public List<Integer> getAllCategoryNum() {
        try {
            List<Integer> Num = violationsService.getVioNum();
            return Num;
        } catch (Exception e) {
            logger.error("获取失败", e);
            return null;
        }
    }

}
